/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 05 - 24
 * Time: 13:43
 */
class ListNode {
    int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class Practice {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) {
            return null;
        }
        while(head != null){
            if(head.val == val){
                head = head.next;
            }
        }
        if(head == null) {
            return null;
        }
        ListNode cur = head.next;
        ListNode th = head;
        while(cur != null) {
            if(cur.val == val) {
                th.next = cur.next;
                cur = cur.next;
            }else {
                th = th.next;
                cur = cur.next;
            }
        }
        return head;
    }
}
